<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\erp\logic;
use think\facade\Hook;
use think\DB;
use util\Tree;
use think\facade\Debug;
use think\facade\Cache;
/**
 * 服务管理
 * @package app\erp\model
 */
class Servicelogic{
	/**
     * 服务列表上轮播
     * @author  shqiang
     * @return array_out(code, message, data)
     */
    public function service_banner($user_id, $store_id){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'user_id' => $user_id,
                'store_id' => $store_id,
            ];
            /********行为参数赋值（*）********/

            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $map = [];
            $map[] = ['store_id', 'in', [0,$store_id]];
			$map[] = ['banner_status', '=', 1];
            $store_banner = model('ErpServiceBanner') -> where($map) -> order('sort asc, store_id desc,create_time asc') -> select();
            $return_list = [];
            foreach($store_banner as $value){
                $return_list[] = [
                    'bannerId' 	=> $value['id'],
                    'imgUrl' 	=> $value['img_url'],
                    'typeId'	=> $value['type_id'],
                ];
            }
            /********业务逻辑********/

            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
            DB::commit();
            return array_out(200, 'success', $return_list);
            /********逻辑返回********/

        } catch (\Exception $e) {
            DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }
	

    /**
     * 服务列表
     * @author  shiqiang
     * @return array_out(code, message, data)
     */
    public function service_list($user_id, $store_id = 0, $service_classid = 0){
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
				'user_id' 	        => $user_id,
				'store_id'      	=> $store_id,
				'service_classid'   => $service_classid
            ];
            /********行为参数赋值（*）********/
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            $map = [];//搜索处理
            $map[] = ['store_id', 'in', [0, $store_id]];
			if($service_classid > 0){
				$map_r = [];
				$map_r[] = ['path', ['like', $service_classid.'-%'], ['like', '%-'.$service_classid], 'or'];
				$class_path = model('ErpServiceClass') -> where($map_r) -> column('id');
				$map[] = ['service_classid', 'in', $class_path];
			}
			
			$list = model('ErpService', 'model') -> where($map) -> order('create_time desc') -> paginate();
			$rel_list = [];
            foreach($list as $key => $value){
                //缩略图
                $cover_id = model('ErpServiceCoverimg') -> where('service_id', $value['id']) -> column('img_id');
                $coverlist = model('ErpServiceImgs') -> where([['id', 'in', $cover_id]]) -> column('id,img_url,width,height');
				$coverlist_arr = [];
				foreach($coverlist as $k => $val){
					$coverlist_arr[] = [
						'id' 		=> $k,
						'img_url' 	=> $val['img_url'],
						'width' 	=> $val['width'],
						'height' 	=> $val['height'],
					];
				}
                //详情图
                $details_id = model('ErpServiceDetails') -> where('service_id', $value['id']) -> column('img_id');
                $detailslist = model('ErpServiceImgs') -> where([['id', 'in', $details_id]]) -> column('id,img_url,width,height');
				$detailslist_arr = [];
				foreach($detailslist as $k => $val){
					$detailslist_arr[] = [
						'id' 		=> $k,
						'img_url' 	=> $val['img_url'],
						'width' 	=> $val['width'],
						'height' 	=> $val['height'],
					];
				}

				$rel_list[$key]['id'] 			    = (int)$value['id'];
				$rel_list[$key]['serviceName'] 	    = (string)$value['service_name'];
				$rel_list[$key]['serviceClassid'] 	= (int)$value['service_classid'];
				$rel_list[$key]['price'] 	        = (string)$value['price'];
				$rel_list[$key]['description'] 	    = (string)$value['description'];
				$rel_list[$key]['store_id'] 	    = (int)$value['store_id'];
				$rel_list[$key]['mobile'] 	   		= (string)$value['mobile'];
				$rel_list[$key]['createTime'] 	    = (int)$value['create_time'];
				$rel_list[$key]['coverImgArr'] 	    = $coverlist_arr;
				$rel_list[$key]['detailsImgArr'] 	= $detailslist_arr;
			}
            /********业务逻辑********/
			
            /********后置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            /********逻辑返回********/
			DB::commit();
            return array_out(200, 'success', $rel_list);
            /********逻辑返回********/
			
        } catch (\Exception $e) {
			DB::rollback();
            return array_out(-9999, $e -> getMessage());//异常错误 -9999
        }
    }
}